
clear all
set matsize 11000
set more off

cd "..\Replication\other\"

insheet using Payments_to_Individual_Agents.csv, comma names
drop if age == 81 | age == 0
save data.dta, replace

collapse (sum) popweights2, by(age)
rename popweights2 count
replace count = round(count*10000)
sort age
save data_temp.dta, replace

use data.dta, clear
sort age
merge m:1 age using data_temp.dta
drop _merge

** Generate amount of revenue received by individual under each policy

*Income Dependent Payments
gen inc_dep_amt = ind_dep_rebate_raw
gen inc_dep_amt_weighted = inc_dep_amt * popweights2

*Pure Labor Progressivity Increase
gen labor_prog_amt = max(0, labor_taxes_prog_old_raw - labor_taxes_prog_raw)
gen labor_prog_amt_weighted = labor_prog_amt * popweights2

** Generate Cumulative Shares of Revenue received by MU

gen mu_raw_descend = -1* mu_raw
sort mu_raw_descend
gen cum_inc_dep_amt_weighted = sum(inc_dep_amt_weighted)
gen cum_labor_prog_amt_weighted = sum(labor_prog_amt_weighted)
gen cum_popweight = sum(popweights2)

sum inc_dep_amt_weighted
scalar define tot_inc_dep_amt_weighted = r(sum)
sum labor_prog_amt_weighted
scalar define tot_labor_prog_amt_weighted = r(sum)

replace cum_inc_dep_amt_weighted = cum_inc_dep_amt_weighted/ tot_inc_dep_amt_weighted
replace cum_labor_prog_amt_weighted = cum_labor_prog_amt_weighted/ tot_labor_prog_amt_weighted
gen cum_lump_sum = cum_popweight


***** Output for Figure 2

line (cum_lump_sum cum_inc_dep_amt_weighted cum_labor_prog_amt_weighted) cum_popweight


***** Output for Table 2

** Compute correlation between MU and payments (conditional and unconditional on working status)
corr mu_raw inc_dep_amt labor_prog_amt [fweight = count]
corr mu_raw inc_dep_amt labor_prog_amt [fweight = count] if age <= 46
corr mu_raw inc_dep_amt labor_prog_amt [fweight = count] if age > 46

** Calculatue share of revenue going towards retired agents
gen retired = age>46
sum popweights2 if retired == 1
scalar define lumpsum_retired_share = r(N)*r(mean)
sum inc_dep_amt_weight if retired == 1
scalar define inc_dep_retired_share = r(N)*r(mean)/tot_inc_dep_amt_weighted

scalar list lumpsum_retired_share inc_dep_retired_share